package com.jindy.module.machine.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.jindy.module.machine.domain.Machine;
import com.jindy.module.machine.domain.MachineRecord;
import com.jindy.module.machine.dto.MachineDto;
import com.jindy.module.machine.service.IMachineRecordService;
import com.jindy.module.machine.service.IMachineService;
import com.jindy.module.order.service.IOrderService;
import com.jindy.module.outboundorder.service.IOutboundOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.common.core.page.TableDataInfo;

/**
 * 机器使用记录Controller
 *
 * @author ruoyi
 * @date 2022-10-21
 */
@RestController
@RequestMapping("/module/machineRecord")
public class MachineRecordController extends BaseController
{
    @Autowired
    private IMachineRecordService machineRecordService;
    @Autowired
    private IOutboundOrderService outboundOrderService;
    @Autowired
    private IOrderService orderService;
    @Resource
    private IMachineService machineService;

    /**
     * 查询机器使用记录列表
     */
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(MachineRecord machineRecord)
    {
        startPage();
        List<MachineRecord> list = machineRecordService.selectMachineRecordList(machineRecord);
        return getDataTable(list);
    }

    /**
     * 查询机器使用记录列表，入库单、出库单、销售订单
     */
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:listByMachine')")
    @GetMapping("/listByMachine")
    public TableDataInfo list(MachineDto machineDto)
    {
        List list = new ArrayList();
        if(machineDto.getSearchType().equals("1")){
            //入库单
            startPage();
            list = machineRecordService.machineListByMachineId(machineDto);
        }else if(machineDto.getSearchType().equals("2")){
            //出库单
            startPage();
            list = outboundOrderService.selectOutboundOrderListByMachineId(machineDto);
        }else if(machineDto.getSearchType().equals("3")){
            //销售订单
            startPage();
            list = orderService.selectOrderListByMachineId(machineDto);
        }
        return getDataTable(list);
    }

    /**
     * 导出机器使用记录列表
     */
    @Log(title = "机器使用记录", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MachineRecord machineRecord)
    {
        List<MachineRecord> list = machineRecordService.selectMachineRecordList(machineRecord);
        ExcelUtil<MachineRecord> util = new ExcelUtil<MachineRecord>(MachineRecord.class);
        util.exportExcel(response, list, "机器使用记录数据");
    }

    /**
     * 获取机器使用记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:id')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(machineRecordService.selectMachineRecordById(id));
    }

    /**
     * 新增机器使用记录
     */
    @Log(title = "机器使用记录", businessType = BusinessType.INSERT)
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:add')")
    @PostMapping
    public AjaxResult add(@RequestBody MachineRecord machineRecord)
    {
        return toAjax(machineRecordService.insertMachineRecord(machineRecord));
    }

    /**
     * 修改机器使用记录
     */
    @Log(title = "机器使用记录", businessType = BusinessType.UPDATE)
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:update')")
    @PutMapping
    public AjaxResult edit(@RequestBody MachineRecord machineRecord)
    {
        return toAjax(machineRecordService.updateMachineRecord(machineRecord));
    }

    /**
     * 删除机器使用记录
     */
    @Log(title = "机器使用记录", businessType = BusinessType.DELETE)
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:delete')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(machineRecordService.deleteMachineRecordByIds(ids));
    }

    /**
     * 根据订单id查询机器
     */
//    @PreAuthorize("@ss.hasPermi('module:machineRecord:machineList')")
    @PostMapping("/machineList")
    public TableDataInfo machineListByOrderId(@RequestBody  MachineRecord machineRecord)
    {
        List<MachineRecord> list = machineRecordService.machineListByOrderId(machineRecord);
        return getDataTable(list);
    }

    /**
     * 根据订单id查询机器列表中的机器
     */
    //    @PreAuthorize("@ss.hasPermi('module:machineRecord:machineList')")
    @PostMapping("/machineList2")
    public TableDataInfo machineListByOrderId2(@RequestBody  MachineRecord machineRecord)
    {
        //根据订单id获取机器记录
        List<MachineRecord> list = machineRecordService.machineListByOrderId(machineRecord);
        List<Machine> list2 = new ArrayList<>();
        list.forEach(record->{
            Machine machine = new Machine();
            //通过机器记录中的机器编号查询机器
            machine.setMachineNo(record.getMachineNo());
            List<Machine> list3 = machineService.selectMachineList(machine);
            Machine machine2 = list3.get(0);
            list2.add(machine2);
        });
        return getDataTable(list2);
    }
}
